home *** CD-ROM | disk | FTP | other *** search
- Path: news.rdc.puc-rio.br!usenet
- From: Paulo Eduardo Neves <neves@lmf-di.puc-rio.br>
- Newsgroups: comp.lang.c++
- Subject: Re: mixing static and const qualifiers for class members
- Date: Thu, 14 Mar 1996 14:31:56 -0600
- Organization: Laboratorio de Metodos Formais - PUC-Rio
- Message-ID: <3148823C.3F54@lmf-di.puc-rio.br>
- References: <4i6mu3$pmg@halon.vggas.com>
- NNTP-Posting-Host: wittgenstein.lmf-di.puc-rio.br
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; AIX 1)
-
- James Youngman wrote:
- >
- > How do I declare and initialise a one-per-class constant? I want to do
- > something like this:
- >
- > class Foo
- > {
- > static const int nPositions = 6;
- > int nData[nPositions];
- > }
- >
- > ...how is this done?
- > James.
-
- To inicialise a ope per class const is easy :
-
- in foo.h :
-
- class Foo {
- static const int nPositions;
- };
-
- in foo.C
-
- const int Foo::nPositions = 6;
-
- Be carefull, if you inicialise it in the header file you will a variable
- in each object file. Besides doing this way you can change the value
- without having to recompile you code.
-
- You also won't be able to declare the array this way, since nPosition
- value will just be know at link time. You will have to do something like
- that :
-
- class Foo {
- Foo():nData(new int[nPositions]){}
- static const int nPositions;
- int *nData;
- };
-
-
- hope this helps,
-
- --
- Paulo Eduardo Neves Laboratorio de Metodos Formais - PUC-Rio
- mailto:neves@lmf-di.puc-rio.br Tel.: (021)512-8045
-